package com.cskt.petproject178.config;


import com.cskt.petproject178.pojo.exception.BaseException;
import com.cskt.petproject178.pojo.vo.StatusEnum;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

import java.io.FileNotFoundException;

/**
 * 微信支付自动配置属性类
 *
 * @author 27013
 **/

@Configuration
@ConfigurationProperties(prefix = "wechat.pay") //配置项与配置类自动映射注解
public class WeChatPayAutoConfigProperties {
    private static final Logger log= LoggerFactory.getLogger(WeChatPayAutoConfigProperties.class);

    // 使用自动更新平台证书的RSA配置
    // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
    @Bean
    public Config config(){
        try {
            privateKeyPath= ResourceUtils.getFile(privateKeyPath).getPath();
        } catch (FileNotFoundException e) {
            log.error("处理私钥路径时出行异常",e);
            throw new BaseException(StatusEnum.COMMON_EXCEPTION);
        }
        Config config =new RSAAutoCertificateConfig.Builder()
              .merchantId(merchantId)
              .merchantSerialNumber(merchantSerialNumber)
              .privateKeyFromPath(privateKeyPath)
              .apiV3Key(apiV3Key)
              .build();
        return config;
    }


    private String appId;
    /** 商户号 */
    private String merchantId;
    /** 商户API私钥路径 */
    private String privateKeyPath;
    /** 商户证书序列号 */
    private String merchantSerialNumber;
    /** 商户APIV3密钥 */
    private String apiV3Key;
    /** 微信服务器回调通知地址*/
    private String notifyUrl;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(String merchantId) {
        this.merchantId = merchantId;
    }

    public String getPrivateKeyPath() {
        return privateKeyPath;
    }

    public void setPrivateKeyPath(String privateKeyPath) {
        this.privateKeyPath = privateKeyPath;
    }

    public String getMerchantSerialNumber() {
        return merchantSerialNumber;
    }

    public void setMerchantSerialNumber(String merchantSerialNumber) {
        this.merchantSerialNumber = merchantSerialNumber;
    }

    public String getApiV3Key() {
        return apiV3Key;
    }

    public void setApiV3Key(String apiV3Key) {
        this.apiV3Key = apiV3Key;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }


}
